package com.csnf.dao;

import com.csnf.entity.Admin;
import com.csnf.util.JdbcUtil;

import java.sql.*;

/**
 * 对admin表的增删改查操作
 */
public class AdminDao {
    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 查询用户通过账号密码
     */
    public Admin getAdminByNamePwd(Admin admin) throws SQLException {
        String sql = "select * from admin where admin_name=? and admin_password=?";
        Connection conn=JdbcUtil.getConnection();
        PreparedStatement ps=JdbcUtil.getPreparedStatement(conn,sql,admin.getAdmin_name(),admin.getAdmin_password());
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            try {
                admin=JdbcUtil.invokeObject(rs,Admin.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return admin;
        }
        return null;
    }
}
